//
// Created by xili on 2024/8/27 17:20.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    long long subArrayRanges(vector<int> &nums) {
        long long ans = 0;
        int n = nums.size();
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < n - i; j++) {
                ans += *max_element(nums.begin() + j, nums.begin() + j + i)
                       - *min_element(nums.begin() + j, nums.begin() + j + i);
            }
        }
        return ans;
    }
};